package com.agriculture.service.impl;

import com.agriculture.common.Response;
import com.agriculture.domain.User;
import com.agriculture.mapper.UserMapper;
import com.agriculture.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    private static final Logger log = LoggerFactory.getLogger(AdminServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public Response<?> getUsers(String keyword, String role, int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        List<User> users = userMapper.findUsers(keyword, role, offset, pageSize);
        int total = userMapper.countUsers(keyword, role);

        Map<String, Object> data = new HashMap<>();
        data.put("list", users);
        data.put("total", total);
        return Response.success("获取成功", data);
    }

    @Override
    @Transactional
    public Response<?> updateUserStatus(Long userId, String status) {
        User user = userMapper.findById(userId);
        if (user == null) {
            return Response.error("用户不存在");
        }
        
        if (!Arrays.asList("active", "disabled").contains(status)) {
            return Response.error("无效的状态值");
        }

        userMapper.updateStatus(userId, status);
        return Response.success("更新状态成功", null);
    }

    @Override
    @Transactional
    public Response<?> deleteUser(Long userId) {
        User user = userMapper.findById(userId);
        if (user == null) {
            return Response.error("用户不存在");
        }
        userMapper.deleteById(userId);
        return Response.success("删除成功", null);
    }

    @Override
    public Response<?> getUserById(Long userId) {
        User user = userMapper.findById(userId);
        if (user == null) {
            return Response.error("用户不存在");
        }
        return Response.success("获取成功", user);
    }

    @Override
    public User getUser(Long userId) {
        return userMapper.findById(userId);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        if (userMapper.checkPhoneNumberExists(user.getPhoneNumber(), user.getUserId()) > 0) {
            throw new RuntimeException("该电话号码已被其他用户使用");
        }

        log.info("Updating user - ID: {}, Phone: {}, Email: {}, Address: {}", 
                user.getUserId(), 
                user.getPhoneNumber(), 
                user.getEmail(), 
                user.getAddress());
        
        int updated = userMapper.update(user);
        log.info("Update result: {}", updated);
        
        if (updated != 1) {
            log.error("Failed to update user: {}", user);
            throw new RuntimeException("更新用户信息失败");
        }
    }
} 